package cn.com.bluemoon.daps.system.utils;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Description 安全工具类;
 * @Author luolq
 * @Date 2022/12/25 11:54
 * @Vsersion 1.0
 **/
@Slf4j
public class SecurityUtils {

    public static String USER_INIT_PWD = "123123";

    /**
     * MD5加密解密
     *
     * @param inStr 明文
     * @return String 密文
     */
    public static String string2Md5(String inStr) {
        if(inStr == null) {
            return null;
        }
        MessageDigest messageDigest;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            log.error("明文加密MD5异常", e);
            return inStr;
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++){
            byteArray[i] = (byte) charArray[i];
        }

        byte[] md5Bytes = messageDigest.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().toUpperCase();
    }
}
